package com.nami.logic.utils;

import com.nami.basic.bo.MqDownMessageWrap;
import com.nami.basic.bo.client.ClientRespBO;
import com.nami.basic.bo.servicetrans.LoginReplyBo;
import com.nami.basic.bo.servicetrans.down.C2CMessageBO;
import com.nami.basic.enums.DownMsgErrorCode;
import com.nami.basic.enums.protocol.DownCMDEnum;
import com.nami.basic.packet.Packet;
import com.nami.logic.serializer.Serializer;
import org.dromara.common.json.utils.JsonUtils;

public class DownWrapperUtil {

    /**
     * @param packet
     * @param identityNo
     * @param errorCode
     * @return
     */
    public static String newC2CWrap(Packet packet, String identityNo, DownMsgErrorCode errorCode) {
        // 组装ws返回内容
        ClientRespBO clientResp = new ClientRespBO();
        clientResp.setCode(errorCode.getCode());
        clientResp.setMsg(errorCode.getMsg());
        packet.setCmd(DownCMDEnum.C2C.getCode());
        packet.setBody(Serializer.prototype(packet.getSerial()).serialize(clientResp));

        // 组装返回gate一层解析使用内容
        C2CMessageBO downTrans = new C2CMessageBO();
        downTrans.setPacket(packet);
        downTrans.setIdentityNo(identityNo);

        MqDownMessageWrap wrap = new MqDownMessageWrap();
        wrap.setCmd(DownCMDEnum.C2C.getCode());
        wrap.setBody(JsonUtils.toJsonByte(downTrans));
        return JsonUtils.toJsonString(wrap);
    }

    /**
     * @param packet
     * @param errorCode
     * @return
     */
    public static String newLoginErrorWrap(Packet packet, String fingerprint, DownMsgErrorCode errorCode) {
        // 组装ws返回内容
        ClientRespBO clientResp = new ClientRespBO();
        clientResp.setCode(errorCode.getCode());
        clientResp.setMsg(errorCode.getMsg());
        packet.setCmd(DownCMDEnum.LOGIN.getCode());
        packet.setBody(Serializer.prototype(packet.getSerial()).serialize(clientResp));

        // 组装返回gate一层解析使用内容

        LoginReplyBo reply = new LoginReplyBo();
        reply.setTempFingerprint(fingerprint);
        reply.setCode(errorCode.getCode());

        MqDownMessageWrap wrap = new MqDownMessageWrap();
        wrap.setCmd(DownCMDEnum.C2C.getCode());
        wrap.setBody(JsonUtils.toJsonByte(reply));
        return JsonUtils.toJsonString(wrap);
    }


}
